package com.glodon.fansi.webui.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glodon.fansi.common.AjaxResult;
import com.glodon.fansi.common.Customer;
import com.glodon.fansi.db.model.FansiWithBLOBs;
import com.glodon.fansi.db.model.User;
import com.glodon.fansi.service.FansiService;
import com.glodon.fansi.service.UserService;

@Controller
@RequestMapping("/fansi")
public class FansiController {
	private static final Logger logger = LoggerFactory.getLogger(FansiController.class);
	@Autowired
	private FansiService fansiService;
	@Autowired
	private UserService userService;

	@RequestMapping("/{account}/admin/new")
	public String newFansi(@PathVariable("account") String account) {
		return "/fansi/new";
	}

	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(FansiWithBLOBs fs) {
		logger.debug("{}", fs);
		fs.setCreatetime(new Date());
		int i = fansiService.save(fs);
		if (i < 0) {
			return AjaxResult.newError();
		}
		return AjaxResult.newSuccsess("信息保存成功");
	}

	@RequestMapping("/{account}/admin/edit")
	public String editFansi(@PathVariable("account") String account, @RequestParam("id") Integer id, ModelMap model) {
		FansiWithBLOBs fs = fansiService.detail(id);
		model.put("fde", fs);
		User u = userService.selectById(fs.getUserId());
		model.put("uname", u.getName());
		return "/fansi/edit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public AjaxResult edit(FansiWithBLOBs fs) {
		int i = fansiService.edit(fs);
		if (i < 0) {
			return AjaxResult.newError();
		}
		return AjaxResult.newSuccsess("信息保存成功");
	}

	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult delete(@RequestParam("id") Integer id) {
		int i = fansiService.delete(id);
		if (i < 0) {
			return AjaxResult.newError();
		}
		return AjaxResult.newSuccsess("信息保存成功");
	}

	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable("id") Integer id, ModelMap model) {
		FansiWithBLOBs fs = fansiService.detail(id);
		if (fs == null) {
			fs = new FansiWithBLOBs();
		}
		User u = userService.selectById(fs.getUserId());
		model.put("fde", fs);
		model.put("uname", u.getName());
		return "/fansi/detailt";
	}

	@RequestMapping({ "/list/{page}" })
	public String list(@PathVariable(value = "page") Integer page, ModelMap model) {
		if (page == null) {
			page = 1;
		}
		logger.debug("the current page is {}", page);
		PageHelper.startPage(page, Customer.PAGE_SIZE);
		List<FansiWithBLOBs> listFs = fansiService.list(null);
		PageInfo<FansiWithBLOBs> pageInfo = new PageInfo<FansiWithBLOBs>(listFs);
		model.put("pageInfo", pageInfo);
		model.put("pageurl", "/fansi/list");
		return "/fansi/list";
	}

	@RequestMapping({ "/list/{account}/{page}" })
	public String mylist(@PathVariable(value = "account") String account, @PathVariable(value = "page") Integer page, ModelMap model, HttpSession session) {
		if (page == null) {
			page = 1;
		}
		logger.debug("the current page is {}", page);
		PageHelper.startPage(page, Customer.PAGE_SIZE);
		Map<String, String> param = new HashMap<String, String>();
		User user = (User) session.getAttribute("user");
		// if(user){
		//
		// }
		param.put("userId", user.getId());
		List<FansiWithBLOBs> listFs = fansiService.list(param);
		PageInfo<FansiWithBLOBs> pageInfo = new PageInfo<FansiWithBLOBs>(listFs);
		model.put("pageInfo", pageInfo);
		model.put("pageurl", "/fansi/list/" + account);
		return "/fansi/mylist";
	}
}
